﻿var iGenChar = 2;
var isFirstGenchart = true;
var table_infor2;
var table_infor2_2;
var indexList = 1;
var chartStyle = 'bar';
var iCheckShowDataTable = false;
$(document).ready(function () {
    $("#aInfor2,#aInfor1").unbind('click');
    $("#aInfor1").click(function () {
        indexList = 1;
        $(".div-infor1").toggle("slow");
        $("select[id$='ddlRegion']").val(0);
    });

    $("#aInfor2").click(function () {
        table_infor2 = $("#table-tmp2-1").clone();
        table_infor2_2 = $("#table-tmp2-2").clone();
        isFirstGenchart = true;
        indexList = 2;
        if ($(".div-infor2").css("display") !== 'none') {
            $(".div-infor2").hide("slow");
            $("fieldset:gt(1)", $(".div-infor2")).remove();
            iGenChar = 2;
        } else {
            $(".div-infor2").show("slow");
            BindData2();
        }
        $("#div-content").html('');
        $("#div-content").html(table_infor2);
        $("#div-content").append(table_infor2_2);
        $("table:eq(0)", $("#div-content")).attr('id', 'tab-infor2');
        $("table:eq(1)", $("#div-content")).attr('id', 'tab-infor2-2');
        $("#tab-infor2").css("display", "none");
    });
    $("#aAddmore").unbind('click');
    $("#aAddmore").click(function () {
        var iCountField = parseInt($("fieldset").length);
        var copy = $("fieldset").eq(parseInt(iCountField - 1)).clone();
        if (iCountField < 5) {

            $("legend", copy).text('Trường thứ ' + parseInt(iCountField + 1));
            $("select", copy).eq(0).attr('id', 'ddlShool2_' + parseInt(iCountField));
            $("select", copy).eq(1).attr('id', 'ddlMajor2_' + parseInt(iCountField));

            $("fieldset").eq(parseInt(iCountField - 1)).after(copy);
        } else {
            alert('Bạn không thể xem hơn 4 trường');
        }
        iGenChar = $("fieldset", $(".div-infor2")).length;
    });
    $("#aRemove").unbind('click');
    $("#aRemove").click(function () {
        var iCountField = parseInt($("fieldset").length);
        if (iCountField > 2) {
            $("fieldset").eq(parseInt(iCountField - 1)).remove();
        } else {
            alert('Bạn không so sánh ít hơn 2 trường');
        }
        iGenChar = $("fieldset", $(".div-infor2")).length;
    });

    $("#aInfor3").unbind('click');
    $("#aInfor3").click(function () {
        indexList = 3;
        $(".div-infor3").toggle("slow");
        $("select[id$='ddlRegion3']").val(0);
    });

    $("#aInfor4").unbind('click');
    $("#aInfor4").click(function () {
        indexList = 4;
        $(".div-infor4").toggle("slow");
    });

    $("#aInfor5").unbind('click');
    $("#aInfor5").click(function () {
        indexList = 4;
        $(".div-infor5").toggle("slow");
    });


    $("#btnShowInfor3").unbind('click');
    $("#btnShowInfor3").click(function () {
        $('.chart-style').show();
        ShowAllOption();
        ShowLoadingBox();
        var iSchoolids = parseInt($("select[id$='ddlSchool3'] option:selected").val());
        var iMajorids = 0;
        var iYears = parseInt($("select[id$='ddlYear3'] option:selected").val());

        $.ajax({
            url: "Default.aspx/GetPercentMajorInSchool",
            type: "POST",
            data: "{iSchoolId:" + iSchoolids + ",iMajorId:" + iMajorids + ",iYear:" + iYears + ",iTop:8}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getInfor3Comp(data); },
            error: function () {
                onError();
            }
        });

    



    });
    $("#btnShowInfor4").unbind('click');
    $("#btnShowInfor4").click(function () {
        $('.chart-style').show();
        ShowAllOption();
        var iGroupId = parseInt($("select[id$='ddlGroup'] option:selected").val());
        var iYear = parseInt($("select[id$='ddlYear4'] option:selected").val());
        var iRegion = parseInt($("select[id$='ddlRegion4'] option:selected").val());

        ShowLoadingBox();
        $.ajax({
            url: "Default.aspx/GetPercentBetweenSchool",
            type: "POST",
            data: "{iGroupId:" + iGroupId + ",iRegion:" + iRegion + ",iYear:" + iYear + ",iTop:8}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getPercentMajorComp(data); },
            error: function () {
                onError();
            }
        });
        
        

    });
    $("#btnShowInfor5").unbind('click');
    $("#btnShowInfor5").click(function () {
        $('.chart-style').show();
        $("#slChart").val('pie');

        $("#slChart option[value='area']").hide();
        $("#slChart option[value='line']").hide();

        
        var iYear = parseInt($("select[id$='ddlYear5'] option:selected").val());
        var iRegion = parseInt($("select[id$='ddlRegion5'] option:selected").val());


        ShowLoadingBox();
        $.ajax({
            url: "Default.aspx/GetPercentFieldBetweenSchool",
            type: "POST",
            data: "{iRegion:" + iRegion + ",iYear:" + iYear + ",iTop:6}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getPercentFieldComp(data); },
            error: function () {
                onError();
            }
        });
      

    });

    $("select[id$='ddlRegion'],select[id$='ddlRegion3']").change(function () {
        var iRegionId = parseInt($(this).val());

        $.ajax({
            url: "Admin/Major.aspx/GetSchoolByRegion",
            type: "POST",
            data: "{iRegion:" + iRegionId + ",isActive:1}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getSchoolComp(data); },
            error: function () {
                onError();
            }
        });
    });

    $("select[id$='ddlSchool1']").change(function () {
        var iSchoolid = $(this).val();
        var iRegionId = parseInt($("select[id$='ddlRegion" + indexList + "'] option:selected").val());
        var sName = '';

        $.ajax({
            url: "Admin/DetailEnrolling.aspx/GetMajor",
            type: "POST",
            data: "{iMajorId:0,sName:'" + sName + "',iSectionId:0,iSchoolId:" + iSchoolid + ",isActive:1,isDelete:0}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getMajorComp(data); },
            error: function () {
                onError();
            }
        });

    });
    $("#btnShowInfor1").unbind('click');
    $("#btnShowInfor1").click(function () {
        $('.chart-style').show();
        ShowAllOption();
        ShowLoadingBox();
        var iSchoolids = parseInt($("select[id$='ddlSchool1'] option:selected").val());
        var iMajorids = parseInt($("select[id$='ddlMajor1'] option:selected").val());
        var frYears = parseInt($("select[id$='fromYear'] option:selected").val());
        var toYears = parseInt($("select[id$='toYear'] option:selected").val());
        var iRegion = parseInt($("select[id$='ddlRegion'] option:selected").val());

        if (parseInt(iMajorids) === -1 || parseInt(iRegion) === 0 || parseInt(iSchoolids) === 0) {
            alert('Bạn chưa chọn đủ điều kiện');
            $("select[id$='ddlMajor1']").focus();
            CloseLoadingBox();
            return false;
        }

        ShowLoadingBox();
        iGenChar = parseInt($("fieldset", $(".div-infor2")).length);
        $.ajax({
            url: "Default.aspx/GetInforMajorSchool",
            type: "POST",
            data: "{iSchoolId:" + iSchoolids + ",iMajorId:" + iMajorids + ",frYear:" + frYears + ",iTo:" + toYears + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getInfor1Comp(data); },
            error: function () {
                onError();
            }
        });

   

    });

    $("select[id^='ddlShool2_']").die('change');
    $("select[id^='ddlShool2_']").live('change', function () {
        var iIdCom = $(this).attr('id');
        var iSchoolid = parseInt($(this).val());
        var isValid = ValidateChooseSchool2(iIdCom, iSchoolid);
        if (parseInt(isValid) === 1) {
            var sName = '';
            var iIndexCom = iIdCom.split('_')[1];
            $.ajax({
                url: "Admin/DetailEnrolling.aspx/GetMajor",
                type: "POST",
                data: "{iMajorId:0,sName:'" + sName + "',iSectionId:0,iSchoolId:" + iSchoolid + ",isActive:1,isDelete:0}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) { getMajorInfoComp2(data, iIndexCom); },
                error: function () {
                    onError();
                }
            });
        } else {
            alert('Bạn đã chọn trường này, xin chọn trường khác');
            if ($("#img-loading").length > 0)
                CloseLoadingBox();
            return false;
            //$("#btnShowInfor2").css('disable', true);
        }



    });

    $("#btnShowInfor2").unbind('click');
    $("#btnShowInfor2").click(function () {
        $('.chart-style').show();
        ShowAllOption();
        var iYear = $("#ddlYear").val();
        $("#div-content").html('');
        $("#div-content").html(table_infor2);
        $("#div-content").append(table_infor2_2);
        $("table:eq(0)", $("#div-content")).attr('id', 'tab-infor2');
        $("table:eq(1)", $("#div-content")).attr('id', 'tab-infor2-2');
        isFirstGenchart = true;
        iGenChar = parseInt($("fieldset", $(".div-infor2")).length);
        $("fieldset", $(".div-infor2")).each(function () {
            var iCurrId = $("select", $(this)).eq(0).attr('id');
            var iCurMajorId = $("select", $(this)).eq(1).attr('id');
            var iSchooseSchool = parseInt($("#" + iCurrId).val());
            var iMajorId = parseInt($("#" + iCurMajorId).val());
            if (parseInt(iMajorId) !== -1 && parseInt(iMajorId) > 0) {
                $.ajax({
                    url: "Default.aspx/GetMajorMultiSchool",
                    type: "POST",
                    async: false,
                    data: "{iMajorId:" + iMajorId + ",iSchoolId:" + iSchooseSchool + ",iYear:" + iYear + "}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) { getMajorInfoSucc2(data); },
                    error: function () {
                        onError();
                    }
                });
            } else {
                alert('Bạn chưa chọn đủ điều kiện');
                $("#" + iCurMajorId).focus();
                if ($("#img-loading").length > 0)
                    CloseLoadingBox();
                return false;
            }


        });
    

    });

    $('.btnShowResult').click(function () {
        //$('.chart-style').show();
        //$('#slChart option:eq(0)').val('bar');

        //$("#numbers option:contains(" + inputText + ")").attr('selected', 'selected');
    });
    $("#slChart").unbind('change');
    $("#slChart").change(function () {
        chartStyle = $(this).val();
        $('.visualize').css('display', 'none');

        $('#tab-infor1-1').visualize({ type: chartStyle, width: '620px' });        
        $('#tab-infor1-2').visualize({ type: chartStyle, width: '620px' });
        $('#tab-infor2').visualize({ type: chartStyle, width: '620px' });
        $('#tab-infor2-2').visualize({ type: chartStyle, width: '620px' });
        $('#tab-infor3').visualize({ type: chartStyle, height: '300px', width: '620px' });
        $('#tab-infor4').visualize({ type: chartStyle, height: '300px', width: '620px' });
        $('#tab-infor5').visualize({ type: chartStyle, height: '300px', width: '620px' });
        
        //$('.visualize').trigger('visualizeRefresh');
    });
    $("#ckDataTable").unbind('click');
    $("#ckDataTable").click(function () {
        iCheckShowDataTable = $(this).is(':checked');
        getShowDataTable();
    });


});

function ShowAllOption() {
    $("#slChart option[value='area']").show();
    $("#slChart option[value='line']").show();
}

function getShowDataTable() {
    iCheckShowDataTable = $("#ckDataTable").is(':checked');    
    if (iCheckShowDataTable === true) {        
        $('.chart-infor1').show('fade');
    }
    else
        $('.chart-infor1').hide('fade');
    
    $("#table-tmp2-1").hide();
    $("#table-tmp2-2").hide();
    
}

function getSchoolComp(data) {
    var sRes = JSON.parse(data.d);
    var List = sRes.lst;
    $("select[id$='ddlSchool" + indexList + "'] option").remove();
    if (List !== null) {
        var iLen = parseInt(List.length);
        if (parseInt(iLen) > 0) {
            for (var i = 0; i < iLen; i++) {
                $("select[id$='ddlSchool" + indexList + "']").append($('<option>').text(List[i].Name).val(List[i].Id));
            }
        }
    }
    $("select[id$='ddlSchool" + indexList + "']").prepend($('<option>').text("-- Chọn trường --").val('0'));
    $("select[id$='ddlSchool" + indexList + "']").val(0);
    ShowSuccess();
}

function getMajorComp(data) {
    var sRes = JSON.parse(data.d);
    var List = sRes.Lst;
    $("select[id$='ddlMajor" + indexList + "'] option").remove();
    if (List !== null) {
        var iLen = parseInt(List.length);
        if (parseInt(iLen) > 0) {
            for (var i = 0; i < iLen; i++) {
                $("select[id$='ddlMajor" + indexList + "']").append($('<option>').text(List[i].Name).val(List[i].Id));
            }
        }
    }
    $("select[id$='ddlMajor" + indexList + "']").prepend($('<option>').text("-- Chọn ngành --").val('-1'));
    $("select[id$='ddlMajor" + indexList + "']").val(-1);

}

function getInfor1Comp(data) {
    chartStyle = $('#slChart').val();
    var sRes = data.d.split('&');
    CloseLoadingBox();
    $("#div-content").html('');
  
    if (sRes[0] === '') {
        $("#div-content").html('Không có dữ liệu');
    }
    else {
        $("#div-content").html(sRes[0]);
        $('#tab-infor1-1').visualize({ type: chartStyle, width: '620px' });
        $("#div-content").append("<br/><br/>");
        $("#div-content").append(sRes[1]);
        $('#tab-infor1-2').visualize({ type: chartStyle, width: '620px' });
    }
    getShowDataTable();
    
}
function onError() {
    alert("Errror");
    ShowError();
}

function BindData2() {

    $.ajax({
        url: "Admin/Major.aspx/GetSchoolByRegion",
        type: "POST",
        data: "{iRegion:0,isActive:1}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) { getSchoolCompInfo2(data); },
        error: function () {
            onError();
        }
    });
}

function getSchoolCompInfo2(data) {
    var sRes = JSON.parse(data.d);
    var List = sRes.lst;
    $("select[id$='ddlShool2_0'] option, select[id$='ddlShool2_1'] option").remove();
    if (List !== null) {
        var iLen = parseInt(List.length);
        if (parseInt(iLen) > 0) {
            for (var i = 0; i < iLen; i++) {
                $("select[id$='ddlShool2_0']").append($('<option>').text(List[i].Name).val(List[i].Id));
                $("select[id$='ddlShool2_1']").append($('<option>').text(List[i].Name).val(List[i].Id));
            }
        }
    }
    $("select[id$='ddlShool2_0']").prepend($('<option>').text("-- Chọn trường --").val('0'));
    $("select[id$='ddlShool2_0']").val(0);
    $("select[id$='ddlShool2_1']").prepend($('<option>').text("-- Chọn trường --").val('0'));
    $("select[id$='ddlShool2_1']").val(0);
}

function getMajorInfoComp2(data, iIndexCom) {
    var sRes = JSON.parse(data.d);
    var List = sRes.Lst;
    $("select[id$='ddlMajor2_" + iIndexCom + "'] option").remove();
    if (List !== null) {
        var iLen = parseInt(List.length);
        if (parseInt(iLen) > 0) {
            for (var i = 0; i < iLen; i++) {
                $("select[id$='ddlMajor2_" + iIndexCom + "']").append($('<option>').text(List[i].Name).val(List[i].Id));
            }
        }
    }
    $("select[id$='ddlMajor2_" + iIndexCom + "']").prepend($('<option>').text("-- Chọn ngành --").val('-1'));
    $("select[id$='ddlMajor2_" + iIndexCom + "']").val(-1);
}

function ValidateChooseSchool2(iIdCom, iSchoolid) {
    var sRes = 1;
    $("fieldset", $(".div-infor2")).each(function () {
        var iCurrId = $("select", $(this)).eq(0).attr('id');
        var iChooseSchool = parseInt($("#" + iCurrId).val());
        if (iChooseSchool === parseInt(iSchoolid) && iIdCom !== iCurrId) {
            sRes = 0;
        }
    });
    return sRes;

}


function getMajorInfoSucc2(data) {
    var sRes = data.d.split('&');

    if (parseInt(iGenChar) === parseInt($("fieldset", $(".div-infor2")).length)) {
        $("#tab-infor2 tbody").html('');
        $("#tab-infor2-2 tbody").html('');
    }
    if (sRes[0] !== '') {
        $("#tab-infor2 tbody").append(sRes[0]);
        $("#tab-infor2").css("display", "");        
    }
    if (sRes[1] !== '') {
        $("#tab-infor2-2 tbody").append(sRes[1]);
        $("#tab-infor2-2").css("display", "");
    }

    if (sRes[0] !== '' && sRes[1] !== '') {
        chartStyle = $('#slChart').val();
        iGenChar = parseInt(iGenChar - 1);
        if (parseInt(iGenChar) === 0 && isFirstGenchart === true) {
            $('#tab-infor2').visualize({ type: chartStyle, width: '620px' });
            $('#tab-infor2-2').visualize({ type: chartStyle, width: '620px' });
            isFirstGenchart = false;
        } else {
            $('.visualize').trigger('visualizeRefresh');
        }
    } else {
        $("#div-content").html('Không có dữ liệu');
    }
    getShowDataTable();

}

function getInfor3Comp(data) {
    chartStyle = $('#slChart').val();
    var sRes = data.d;
    if (sRes === '') {
        $("#div-content").html('Không có dữ liệu');
    }
    else {
        $("#div-content").html(sRes);
        $('#tab-infor3').visualize({ type: chartStyle, height: '300px', width: '620px' });    
    }
    getShowDataTable();
}

function getPercentMajorComp(data) {
    chartStyle = $('#slChart').val();
    var sRes = data.d;
    $("#div-content").html('');
    if (sRes === '')
        $("#div-content").html('Không có dữ liệu');
    else {
        $("#div-content").html(sRes);
        $('#tab-infor4').visualize({ type: chartStyle, height: '300px', width: '620px' });
    }
    getShowDataTable();
}

function getPercentFieldComp(data) {
    chartStyle = $('#slChart').val();
    
    var sRes = data.d;
    $("#div-content").html('');
    if (sRes === '')
        $("#div-content").html('Không có dữ liệu');
    else {
        $("#div-content").html(sRes);        
        $('#tab-infor5').visualize({ type: chartStyle, height: '300px', width: '620px' });
    }
    getShowDataTable();
}